//
//  NetworkTools.swift
//  RacWeibo
//
//  Created by hanxu on 15/10/19.
//  Copyright © 2015年 hanxu. All rights reserved.
//

import UIKit
import AFNetworking
import ReactiveCocoa

enum requestMethod: String {
    case GET = "GET"
    case POST = "POST"
}

class NetworkTools: AFHTTPSessionManager {

    
    private let clientId = "1640861112"
    private let App_Secret = "78fd9ed339f5ddac78f80796479fe188"
    //call Back
    let redirectUri = "http://itheima.com"

    //singleton
    static let sharedTools : NetworkTools = {
       
        var instance = NetworkTools(baseURL:nil)
        
        instance.responseSerializer.acceptableContentTypes.insert("text/plain")
        
        return instance
    }()
    
    var OAuth : NSURL {
        let urlString = "https://api.weibo.com/oauth2/authorize?client_id=\(clientId)&redirect_uri=\(redirectUri)"
        return NSURL(string: urlString)!
    }
    
    ///  获取用户的token
    ///
    ///  - parameter code: 验证码
    ///
    ///  - returns: 携带token的信号
    func loadAccessToken(code: String) -> RACSignal {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        
        let parameters = [
            "client_id"         : clientId,
            "client_secret"     : App_Secret,
            "grant_type"        : "authorization_code",
            "code"              : code,
            "redirect_uri"      : redirectUri]
        
        return request(.POST, urlString: urlString, parameter: parameters)
    }
    
    ///  根据token获取用户信息
    ///
    ///  - parameter token: token
    ///
    ///  - returns: RACSignal
    func loadUserInfo(token: String, uid: String) -> RACSignal {
        let urlString = "https://api.weibo.com/2/users/show.json"
        
        let parameters = [
            "access_token" : token,
            "uid" : uid]
        
        return request(.GET, urlString: urlString, parameter: parameters)
    }
    
    ///  网络请求的基本方法
    ///
    ///  - parameter method:    GET\POST
    ///  - parameter urlString: urlString
    ///  - parameter parameter: parameter
    ///
    ///  - returns: return a signal contains success\failure block
    private func request(method: requestMethod, urlString: String, parameter: [String: AnyObject]?) -> RACSignal {
        
        return RACSignal.createSignal() { (subscriber) -> RACDisposable! in
            
            let successCallBack = { (task: NSURLSessionDataTask!, result:AnyObject!) -> Void in
                printLog(result)
                subscriber.sendNext(result)
                subscriber.sendCompleted()
            }
            let failurCallBack = { (task: NSURLSessionDataTask!, error: NSError!) -> Void in
                printLog(error)
                subscriber.sendNext(error)
                subscriber.sendCompleted()
            }
            
            if method == requestMethod.GET {
                self.GET(urlString, parameters: parameter, success: successCallBack, failure: failurCallBack)
            } else {
                self.POST(urlString, parameters: parameter, success: successCallBack, failure: failurCallBack)
            }
            return nil
        }
        
    }
    
}
